import React from 'react';

interface State {
  component: any;
}

let state: State = {
  component: '',
};
/**
 * 异步加载组件
 * @param {*} importComponent
 */

export default function asyncComponent(importComponent: Function) {
  class AsyncComponent extends React.Component {
    state = state;
    constructor(props: any) {
      super(props);
      this.init();
    }
    init = () => {
      importComponent().then((res: any) => {
        this.setState({ component: res.default });
      });
    };
    componentWillUnmount() {
      this.setState = () => {};
    }
    render() {
      const C = this.state.component;
      return C ? <C {...this.props} /> : null;
    }
  }
  return AsyncComponent;
}
